import RouterLink from "./components/router-link";
import RouterView from "./components/router-view";
export let Vue;

export function install(_Vue) {
    Vue = _Vue;
    Vue.mixin({
        //利用mixin为所有使用router的实例 添加router属性
        beforeCreate() {
            if (this.$options.router) {
                this._rootRouter = this;
                this._router = this.$options.router;
                this._router.init(this);
                // 调用vue工具方法 为实例添加_route属性  属性值为current  相当于将current变为响应式
                Vue.util.defineReactive(this, "_route", this._router.history.current);
            } else {
                // 取父级的_router
                // this._router = this.$parent._router
                this._rootRouter = this.$parent?._rootRouter;
            }
        },
    });

    Object.defineProperty(Vue.prototype, "$route", {
        get() {
            return this._rootRouter._route;
        },
    });

    // 为所有组件添加一个$router属性，=== current属性
    Object.defineProperty(Vue.prototype, "$router", {
        get() {
            return this._rootRouter?._router;
        },
    });

    Vue.component("router-link", RouterLink);

    Vue.component("router-view", RouterView);
}
